﻿using SignInServer.SQL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.Project.Info;
using static WindowsFormsApp1.Project.Info.ActivityInfo;

namespace WindowsFormsApp1.SQL
{
    class SQLActivityAccess
    {
        public static ActivityInfo RegisterActivity(ActivityInfo activityInfo)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("@ActivityInitiatorID",activityInfo.activityInitiatorID),
                            new SqlParameter("@ActivityTheme",activityInfo.activityTheme),
                            new SqlParameter("@ActivityLongitude",activityInfo.activityCheckInLongitude),
                            new SqlParameter("@ActivityLatitude",activityInfo.activityCheckInLatitude),
                            new SqlParameter("@ActivityInvitationCode",activityInfo.activityInvitationCode),
                            new SqlParameter("@ActivityCheckInStartTime",activityInfo.activityCheckInStartTime),
                            new SqlParameter("@ActivityCheckInEndTime",activityInfo.activityCheckInEndTime),
                            new SqlParameter("@ActivityStartTime",activityInfo.activityStartTime),
                            new SqlParameter("@ActivityEndTime",activityInfo.activityEndTime),
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("RegisterActivity", CommandType.StoredProcedure, p);
            if (dr.Read())
            {
                activityInfo.activityID = Convert.ToInt32(dr["ActivityID"]);
                Console.WriteLine(" SQL用户注册活动-用户ID" + activityInfo.activityInitiatorID + ";活动ID:" + activityInfo.activityID);
            }
            dr.Close();
            return activityInfo;
        }

        //根据活动id获取活动参与者
        //public static int GetActivityParticipantInfo(int activityID)
        //{
        //    SqlParameter[] p = new SqlParameter[]
        //    {
        //           new SqlParameter("@ActivityID",activityID)
        //    };
        //    SqlDataReader dr = SQLHelper.ExecuteReader("GetNumberOfPeopleJoinActivity", CommandType.StoredProcedure, p);
        //    int a = 0;
        //    if (dr.Read())
        //    {
        //        a = Convert.ToInt32(dr["Number"]);
        //    }
        //    dr.Close();
        //    return a;
        //}

        public static bool ValidateActivityInvitationCode(int activity,string code)
        {
            SqlParameter[] p = new SqlParameter[]
            {
                   new SqlParameter("@ActivityID",activity),
                   new SqlParameter("@ActivityInvitationCode",code)
            };
            SqlDataReader dr = SQLHelper.ExecuteReader("ValidateActivityInvitationCode", CommandType.StoredProcedure, p);
            int a = -1;
            if (dr.Read())
            {
                a = Convert.ToInt32(dr["ActivityID"]);
            }
            dr.Close();
            return a!=-1;
        }

        public static bool JionInActivity(int activityID,int userID,string code)
        {
            if (ValidateActivityInvitationCode(activityID, code))
            {
                SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("@ActivityID",activityID),
                            new SqlParameter("@UserID",userID)
                     };
                int i = SQLHelper.ExecuteNonQuery("JoinInActivity", CommandType.StoredProcedure, p);
                Console.WriteLine(" SQL用户加入活动-用户ID" + userID + ";活动ID:" + activityID);
                return i > 0;
            }
            else
            {
                return false;
            }
        }

        public static ActivityInfo GetActivityInfoByActivityID(int activityID)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("@ActivityID",activityID)
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("GetActivityInfoByActivityID", CommandType.StoredProcedure, p);
            ActivityInfo activityInfo = null;
            if (dr.Read())
            {
                activityInfo = new ActivityInfo(activityID,Convert.ToInt32(dr["ActivityInitiatorID"]),Convert.ToString(dr["ActivityTheme"]),
                    Convert.ToDouble(dr["ActivityLongitude"]), Convert.ToDouble(dr["ActivityLatitude"]),Convert.ToString(dr["ActivityInvitationCode"]),
                    DateTime.Parse(Convert.ToString(dr["ActivityCheckInStartTime"])), DateTime.Parse(Convert.ToString(dr["ActivityCheckInEndTime"])),
                    DateTime.Parse(Convert.ToString(dr["ActivityStartTime"])), DateTime.Parse(Convert.ToString(dr["ActivityEndTime"])));
            }
            dr.Close();
            Console.WriteLine(" SQL获取活动信息-ID" + activityID + ";");
            return activityInfo;
        }

        public static ActivityInfo GetJoinedActivityListByActivityID(int activityID)
        {
            SqlParameter[] p = new SqlParameter[]
                     {
                            new SqlParameter("ActivityID",activityID)
                     };
            SqlDataReader dr = SQLHelper.ExecuteReader("GetJoinedActivityListByActivityID", CommandType.StoredProcedure, p);
            List<Participant> participantActivityList = new List<Participant>();
            while (dr.Read())
            {
                Participant participant=new Participant(Convert.ToInt32(dr["UserID"]),Convert.ToBoolean(dr["ClockIn"]),Convert.ToBoolean(dr["Administrator"]));
                participantActivityList.Add(participant);
            }
            dr.Close();
            ActivityInfo activityInfo = new ActivityInfo(participantActivityList,activityID);
            Console.WriteLine(" SQL获取活动参与用户-ID" + activityID + ";");
            return activityInfo;
        }
    }
}
